<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\websocket\handler;

use app\common\services\system\admin\AdminAuthServices;
use app\websocket\BaseHandler;
use app\websocket\Response;
use hdphp\exceptions\AuthException;

/**
 * Class：AdminHandler
 * 创建人：Jack
 * 创建时间：2024/1/13 8:33
 * @package app\websocket\handler
 */
class AdminHandler extends BaseHandler
{
    /**
     * 注释：后台登陆
     * 创建人：Jack
     * 创建时间：2024/1/13 8:33
     * @param array    $data
     * @param Response $response
     * @return mixed
     */
    public function login(array $data, Response $response)
    {
        if (!isset($data['token']) || !$token = $data['token']) {
            return $response->fail('授权失败!');
        }

        try {
            /** @var AdminAuthServices $adminAuthService */
            $adminAuthService = app()->make(AdminAuthServices::class);
            $authInfo = $adminAuthService->parseToken($token);
        } catch (AuthException $e) {
            return $response->fail($e->getMessage());
        }

        if (!$authInfo || !isset($authInfo['id'])) {
            return $response->fail('授权失败!');
        }

        return $response->success(['uid' => $authInfo['id']]);
    }

}